<template>
  <el-row justify="center" style="margin-top: 50px">
    <el-space wrap>
      <span>角色名称</span>
      <el-input
        v-model="roleName"
        style="width: 240px"
        placeholder="请输入角色名称"
        :suffix-icon="Calendar"
      />
      <el-button type="primary" @click="handlerQuery">查询</el-button>
      <el-button type="success" @click="showAddDialog">添加</el-button>
    </el-space>
  </el-row>
  <el-dialog v-model="dialogFormVisible" title="添加用户" width="500">
    <el-form :model="form">
      <el-form-item label="角色名称" :label-width="formLabelWidth">
        <el-input v-model="form.roleName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="form.remark" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleAdd"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
  <el-row justify="center" style="margin-top: 50px">
    <el-table :data="tableData.data.records" stripe style="width: 90%; margin-top: 50px" border>
      <el-table-column prop="roleName" label="角色名称" width="180" align="center" />
      <el-table-column prop="remark" label="备注" align="center" />
      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" circle @click="queryById(scope.row.roleId)" />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            @click="handleDelete(scope.$index, scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
  </el-row>
  <el-row justify="center" style="margin-top: 50px">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="tableData.data.total"
      :default-page-size="pageSize"
      @prev-click="prePage"
      @next-click="nextPage"
      @current-change="goto"
    ></el-pagination>
  </el-row>
</template>
<script setup>
import { roles } from '../../../http'
import { ref, onMounted } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
var pageNo = 1
var pageSize = 10
var doUpdate = false
var roleName = ref('')
var dialogFormVisible = ref(false)
var form = ref({
  roleName: '',
  remark: ''
})
var tableData = ref({
  code: 0,
  data: {
    records: [],
    total: 0,
    size: 2,
    current: 1,
    orders: [],
    optimizeCountSql: true,
    hitCount: false,
    countId: null,
    maxLimit: null,
    searchCount: true,
    pages: 15
  },
  msg: '执行成功'
})
onMounted(() => {
  handlerQuery()
})

const queryById = async (id) => {
  doUpdate = true
  var result = await roles.queryById(id)
  if (result.code == 0) {
    dialogFormVisible.value = true
    form.value = result.data
  }
}
const showAddDialog = () => {
  doUpdate = false
  dialogFormVisible.value = true
}
const handleAdd = async () => {
  var result = {}
  if (doUpdate) {
    result = await roles.update(form.value)
    if (result.code == 0) {
      ElMessage.success('修改成功！')
      dialogFormVisible.value = false
      handlerQuery(pageNo, pageSize, roleName.value)
      form = ref({})
    }
  } else {
    result = await roles.add(form.value)
    if (result.code == 0) {
      ElMessage.success('添加成功！')
      dialogFormVisible.value = false
      handlerQuery(pageNo, pageSize, roleName.value)
      form = ref({})
    }
  }
}
const handleDelete = async (index, row) => {
  var result = await roles.deleteUser(row.roleId)
  console.log(result)
  if (result.code == 0) {
    ElMessage.success('删除成功！')
    tableData.value.data.records.splice(index, 1)
  }
}
const handlerQuery = async () => {
  const result = await roles.queryList(pageNo, pageSize, roleName.value)
  console.log(result)
  if (result.code == 0) {
    tableData.value = result
  }
}
const prePage = () => {
  pageNo--
  handlerQuery(pageNo, pageSize, roleName.value)
}
const nextPage = () => {
  pageNo++
  handlerQuery(pageNo, pageSize, roleName.value)
}
const goto = (no) => {
  pageNo = no
  handlerQuery(pageNo, pageSize, roleName.value)
}
</script>
